package com.coder.books.thinkinjava.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import static com.coder.util.SwingConsole.run;

/**
 *
 * @author yuhushuan
 * @date 2020/9/7 11:27
 */
public class Button2 extends JFrame {
    private JButton b1 = new JButton("Button 1"),
            b2 = new JButton("Button 2");
    private JTextField txt = new JTextField(10);

    class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = ((JButton) e.getSource()).getText();
            txt.setText(name);
        }
    }
    private ButtonListener bl = new ButtonListener();

    public Button2() {
        setLayout(new FlowLayout());
        add(b1);
        add(b2);
        add(txt);

        b1.addActionListener(bl);
        b2.addActionListener(bl);
    }

    public static void main(String[] args) {
        run(new Button2(), 200, 150);
    }
}
